package com.daiyang.lottery.controller;

import com.daiyang.lottery.service.enums.UserIdentityEnum;
import com.daiyang.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.daiyang.lottery.common.exception.ControllerException;
import com.daiyang.lottery.common.pojo.CommonResult;
import com.daiyang.lottery.common.util.JacksonUtil;
import com.daiyang.lottery.controller.param.ShortMessageLoginParam;
import com.daiyang.lottery.controller.param.UserPasswordLoginParam;
import com.daiyang.lottery.controller.param.UserRegisterParam;
import com.daiyang.lottery.controller.result.UserBaseInfoResult;
import com.daiyang.lottery.controller.result.UserLoginResult;
import com.daiyang.lottery.controller.result.UserRegisterResult;
import com.daiyang.lottery.service.UserService;
import com.daiyang.lottery.service.VerificationCodeService;
import com.daiyang.lottery.service.dto.UserDTO;
import com.daiyang.lottery.service.dto.UserLoginDTO;
import com.daiyang.lottery.service.dto.UserRegisterDTO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class UserContoller {
    private static final Logger logger = LoggerFactory.getLogger(UserContoller.class);
    @Resource(name = "userServiceImpl")
    private UserService userService;
    @Resource(name = "verificationCodeServiceImpl")
    private VerificationCodeService verificationCodeService;
    //@PostMapping("/register")
    @RequestMapping("/register")
    public CommonResult<UserRegisterResult> register(@Validated @RequestBody UserRegisterParam userRegisterParam) {
        logger.info("UserContoller UserRegisterParam {}", JacksonUtil.writeValueAsString(userRegisterParam) );
        UserRegisterDTO userRegisterDTO=userService.register(userRegisterParam);

        return CommonResult.success(convertToUserRegisterResult(userRegisterDTO));
    }
    @RequestMapping("/verification-code/send")
    public CommonResult<Boolean> sendVerificationCode(@Validated String phoneNumber) {
        logger.info("UserContoller sendVerificationCode pohneNumber{}", phoneNumber);
        //TODO 发送验证码
        verificationCodeService.sendVerificationCode(phoneNumber);
        return CommonResult.success(Boolean.TRUE);
    }
    @RequestMapping("/password/login")
    public CommonResult<UserLoginResult> login(@Validated @RequestBody UserPasswordLoginParam param) {
        logger.info("UserContoller UserPasswordLoginParam {}", JacksonUtil.writeValueAsString(param) );

        UserLoginDTO userLoginDTO= userService.login(param);

        return CommonResult.success(convertToUserLoginResult(userLoginDTO));

    }
    @RequestMapping("/message/login")
    public CommonResult<UserLoginResult> login(@Validated @RequestBody ShortMessageLoginParam param) {
        logger.info("UserContoller ShortMessageLoginParamLogin {}", JacksonUtil.writeValueAsString(param) );
        UserLoginDTO userLoginDTO= userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }
    @RequestMapping("/base-user/find-list")
    public CommonResult<List<UserBaseInfoResult>> findUserBaseInfoList(String identity) {
        logger.info("UserContoller findBaseUserList:{}", identity);
        List<UserDTO> userDTOList =null;
        if(!StringUtils.hasText(identity)){
            userDTOList =userService.findUserList(null);
        } else if(UserIdentityEnum.forName(identity)!=null)  {
            userDTOList=userService.findUserList(UserIdentityEnum.forName(identity));
        }
        return CommonResult.success(convertToUserListResult(userDTOList));


    }

    private List<UserBaseInfoResult> convertToUserListResult(List<UserDTO> userDTOList) {
        if(CollectionUtils.isEmpty(userDTOList)){
            return Arrays.asList();
        }
        return userDTOList.stream()
                .map(userDTO -> {
                    UserBaseInfoResult userBaseInfoResult=new UserBaseInfoResult();
                    userBaseInfoResult.setUserName(userDTO.getUserName());
                    userBaseInfoResult.setUserId(userDTO.getId());
                    userBaseInfoResult.setIdentity(userDTO.getIdentity().name());
                    return  userBaseInfoResult;
                }).collect(Collectors.toList());
    }

    private UserLoginResult convertToUserLoginResult(UserLoginDTO userLoginDTO) {
        if(null==userLoginDTO){
            throw new ControllerException(ControllerErrorCodeConstants.LOGIN_ERROR);
        }
        UserLoginResult userLoginResult=new UserLoginResult();
        userLoginResult.setIdentity(userLoginDTO.getIdentity().name());
        userLoginResult.setToken(userLoginDTO.getToken());
        return userLoginResult;
    }

    private UserRegisterResult convertToUserRegisterResult(UserRegisterDTO userRegisterDTO) {
        if(null==userRegisterDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.REGISTER_ERROR);
        }
        UserRegisterResult userRegisterResult=new UserRegisterResult();
        userRegisterResult.setUserId(userRegisterDTO.getUserId());
        return userRegisterResult;
    }
}
